<%@ page language="java" errorPage="/error.jsp" pageEncoding="UTF-8"
	contentType="text/html;charset=utf-8"%>
<script type="text/javascript">
	var setting = {
		async: {
			enable: true,
			url:"getDictTree.action",
			autoParam:["dictId"]
		},	
		view : {
			addHoverDom : addHoverDom,
			removeHoverDom : removeHoverDom,
			selectedMulti : false
		},
		edit : {
			enable : true,
			showRenameBtn: false
		},
		
		callback : {
			beforeDrag : beforeDrag,
			beforeRemove : beforeRemove,
			beforeRename : beforeRename,
			onRemove : onRemove,
			onRename : onRename
		}
	};

	var log, className = "dark";
	function beforeDrag(treeId, treeNodes) {
		return false;
	}
	function beforeRemove(treeId, treeNode) {
		return confirm("确认删除 节点 -- " + treeNode.name + " 吗？");
	}
	function onRemove(e, treeId, treeNode) {
		$('#currentDictId').val(treeNode.dictId);
		$('#myform').form('submit',{
			url:'removeDict.action',
			onSubmit:function(){return $(this).form('validate');},
			success:function(){
				$.fn.zTree.init($("#dictTree"), setting);
			}
		});
	}
	function beforeRename(treeId, treeNode, newName) {
		return true;
	}
	function onRename(e, treeId, treeNode) {
	}
	function showLog(str) {
		if (!log)
			log = $("#log");
		log.append("<li class='"+className+"'>" + str + "</li>");
		if (log.children("li").length > 8) {
			log.get(0).removeChild(log.children("li")[0]);
		}
	}
	function getTime() {
		var now = new Date(), h = now.getHours(), m = now.getMinutes(), s = now
				.getSeconds(), ms = now.getMilliseconds();
		return (h + ":" + m + ":" + s + " " + ms);
	}

	var newCount = 1;
	function addHoverDom(treeId, treeNode) {
		var sObj = $("#" + treeNode.tId + "_span");
		if ($("#addBtn_" + treeNode.id).length > 0)
			return;
		var addStr = "<button type='button' class='icon_add'  id='addBtn_"
				+ treeNode.id
				+ "' title='新增子节点' onfocus='this.blur();'></button>"
				+ "<button type='button' class='icon_edit'  id='editBtn_"
				+ treeNode.id
				+ "' title='编辑子节点' onfocus='this.blur();'></button>";
		sObj.append(addStr);
		var btn = $("#addBtn_" + treeNode.id);
		var editBtn = $("#editBtn_" + treeNode.id);
		if (btn)
			btn.bind("click", function() {
				$('#myDialog').dialog('setTitle', '字典信息').dialog('open');
				$('#currentDictId').val(treeNode.dictId);
			});
			editBtn.bind("click", function() {
			$('#dictName').val(treeNode.name);
			$('#moduleName').val(treeNode.moduleName);
			$('#sort').val(treeNode.sort);
			$('#dictRemark').val(treeNode.dictRemark);
			$('#leaf').val(treeNode.leaf);
			$('#parentDictId').val(treeNode.parentDictId);
			$('#dictId').val(treeNode.dictId);
			$('#currentDictId').val(treeNode.dictId);
			$('#myDialog').dialog('setTitle', '字典信息').dialog('open');
		});
	};
	function removeHoverDom(treeId, treeNode) {
		$("#addBtn_" + treeNode.id).unbind().remove();
		$("#editBtn_" + treeNode.id).unbind().remove();
	};

	function saveItem(){
		$('#myform').form('submit',{
			url:"saveDict.action",
			onSubmit:function(){return $(this).form('validate');},
			success:function(){
				$('#myDialog').dialog('close');
				alert("保存成功");
				$('#currentDictId').val("");
				$('#dictId').val("");
				$('#parentDictId').val("");
				$('#dictName').val("");
				$('#leaf').val("");
				$('#moduleName').val("");
				$('#sort').val("");
				$('#dictRemark').val("");
				 resetTree();
			}
		});
	}
	
	function resetTree() {
		$.fn.zTree.init($("#dictTree"), setting);
	}
	
	$(document).ready(function() {
		//Init Ztree
		$.fn.zTree.init($("#dictTree"), setting);
		//Init jquery dialog
	});
</script>
<style>
.header-dict{
	background: url("image/header-dict.gif") no-repeat;
	height: 39px;
	margin-bottom: 20px;
}
</style>
<!-- EasyUi dialog for Add operation -->
<div class="header-dict"></div>
<div id="myDialog" class="easyui-dialog"
	style="width: 300px;" closed="true" modal="true"
	buttons="#dlg-buttons">
	<form id="myform" method="post">
		<div style="padding: 20px">
			<table cellpadding="0" cellspacing="0" class="form-table">
				<input type="hidden" id="currentDictId" name="currentDictId"/>
				<input type="hidden" id="dictId" name="dictId"/>
				<input type="hidden" id="parentDictId" name="parentDictId"/>
				<input type="hidden" id="leaf" name="leaf"/>
				<tr>
					<td>字典名称</td>
					<td><input id="dictName" name="dictName" style="width: 150px"></input>
					</td>
				</tr>
				<tr>
					<td>模块名称</td>
					<td><input  id="moduleName" name="moduleName" style="width: 150px"></input>
					</td>
				</tr>
				<tr>
					<td>排序号</td>
					<td><input id="sort" name="sort" style="width: 150px"></input>
					</td>
				</tr>
				<tr>
					<td>说明</td>
					<td><textarea id="dictRemark" name="dictRemark" style="width: 150px"></textarea>
					</td>
				</tr>
			</table>
		</div>
	</form>
	<div id="dlg-buttons" style="text-align: center">
		<a href="#" class="easyui-linkbutton" iconCls="icon-save"
			onclick="saveItem()">保存</a> <a href="#"
			class="easyui-linkbutton" iconCls="icon-cancel"
			onclick="javascript:$('#myDialog').dialog('close')">取消</a>
	</div>
</div>

<div class="content_wrap">
	<div class="zTreeDemoBackground left">
		<ul id="dictTree" class="ztree"></ul>
	</div>
</div>